package com.wavehi.service.impl;

import com.wavehi.mapper.IUserMapper;
import com.wavehi.pojo.User;
import com.wavehi.service.IUserService;
import com.wavehi.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Author WaveHi
 * @Date 25/9/2021 上午 11:04
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
public class UserServiceImpl implements IUserService {
    /**
     * 添加用户
     * @param user  需要添加的用户
     * @return 返回false表示添加失败
     */
    public Boolean add(User user) {
        SqlSession session = null;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //添加
            Boolean isCompleted = mapper.insert(user);
            return isCompleted;
        } catch (Exception e) {
            e.printStackTrace();
            //异常回滚
            if(session != null) {
                session.rollback();
                session.close();
                session = null;
            }
            return false;
        } finally {
            //资源的关闭
            if(session != null) {
                session.commit();
                session.close();
                session = null;
            }
        }
    }

    /**
     * 更新用户信息
     * @param user 需要更新的信息
     * @return 返回false表示更新失败
     */
    public Boolean updateUser(User user) {
        SqlSession session = null;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //更新
            Boolean isCompleted = mapper.update(user);
            return isCompleted;
        } catch (Exception e) {
            e.printStackTrace();
            //异常回滚
            if(session != null) {
                session.rollback();
                session.close();
                session = null;
            }
            return false;
        } finally {
            //资源的关闭
            if(session != null) {
                session.commit();
                session.close();
                session = null;
            }
        }
    }

    /**
     * 删除用户
     * @param id 用户对应的id
     * @return 返回false表示删除失败或没有该用户
     */
    public Boolean deleteUser(Long id) {
        SqlSession session = null;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //删除
            Boolean isCompleted = mapper.deleteById(id);
            return isCompleted;
        } catch (Exception e) {
            e.printStackTrace();
            //异常回滚
            if(session != null) {
                session.rollback();
                session.close();
                session = null;
            }
            return false;
        } finally {
            //资源的关闭
            if(session != null) {
                session.commit();
                session.close();
                session = null;
            }
        }
    }

    /**
     * 判断用户名是否已经存在
     * @param username 用户名
     * @return 返回true表示用户名已存在
     */
    public Boolean userExists(String username) {
        SqlSession session = null;
        boolean exists = false;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //查询
            User user = mapper.selectByUsername(username);
            //判断
            if(user != null) {
                exists = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
            return exists;
        }
    }

    /**
     * 根据用户名和密码判断用户是否存在
     * @param username 用户名
     * @param password 密码
     * @return 返回false表示用户不存在
     */
    public Boolean userExists(String username, String password) {
        SqlSession session = null;
        boolean exists = false;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //查询
            User user = mapper.selectByUsernameAndPassword(username, password);
            //判断
            if(user != null) {
                exists= true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
            return exists;
        }
    }

    @Override
    public User getUser(Long id) {
        SqlSession session = null;
        User user = null;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //查询
            user = mapper.selectById(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
            return user;
        }
    }

    @Override
    public User getUser(String username) {
        SqlSession session = null;
        User user = null;
        try {
            //获取数据库连接
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //查询
            user = mapper.selectByUsername(username);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
            return user;
        }
    }

    /**
     * 获取所有用户
     * @return 返回一个包含所有用户的List集合
     */
    public List<User> getAllUsers() {
        SqlSession session = null;
        List<User> users = null;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //查询
            users = mapper.selectAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
            return users;
        }
    }

    /**
     * 获取指定页和指定数量的数用户
     * @param pageNo 页数
     * @param dataCount 数据量
     * @return 返回一个包含指定数量用户的List集合
     */
    public List<User> getPartOfUsers(Integer pageNo, Integer dataCount) {
        SqlSession session = null;
        List<User> users = null;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //查询
            int offset = dataCount * (pageNo - 1);
            users = mapper.selectByLimit(offset, dataCount);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
            return users;
        }
    }

    @Override
    public List<User> getPartOfUsers(User user) {
        SqlSession session = null;
        List<User> users = null;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //查询
            users = mapper.selectByCondition(user);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
            return users;
        }
    }

    @Override
    public List<User> getPartOfUsers(User user, Integer pageNo, Integer dataCount) {
        SqlSession session = null;
        List<User> users = null;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            //查询
            int offset = (pageNo - 1) * dataCount;
            users = mapper.selectByConditionAndLimit(user, offset, dataCount);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
            return users;
        }
    }
}
